//
//  CXYSongsMenuViewController.m
//  NewMusic
//
//  Created by chuxinyu123 on 15/11/21.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "CXYSongsMenuViewController.h"
#import "CXYSongsMenuTableViewCell.h"

#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
@interface CXYSongsMenuViewController ()<UITableViewDataSource,
UITableViewDelegate,
CXYSongsMenuTableViewCellDelegate>
@property (nonatomic, retain) NSUserDefaults *user;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UIButton *buttonAll;
@end


@implementation CXYSongsMenuViewController

- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.user = [NSUserDefaults standardUserDefaults];
    [self buildTableView];
}

- (void)buildTableView
{
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(10, 20, 60, 30);
    [self.button setTitle:@"取消" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(disAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.buttonAll = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonAll.frame = CGRectMake((375 - 175) / 2, 20, 175, 30);
    [self.buttonAll setTitle:@"全部" forState:UIControlStateNormal];
    NSString *string = [self.user objectForKey:@"buttonText"];
    if ([string isEqual:@"全部"]) {
        [_buttonAll setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    }else{
        [_buttonAll setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    [self.buttonAll addTarget:self action:@selector(disMissAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 667) style:UITableViewStyleGrouped];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.bounces = NO;
    [self.view addSubview:_tableView];
    [self.view addSubview:_buttonAll];
    [self.view addSubview:_button];
    [_tableView release];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CXYSongsMenuTableViewCell *cell = [[CXYSongsMenuTableViewCell alloc] init];
    cell.delegate = self;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 667 - 64;
}

- (void)popSongMenuController:(NSString *)title
{
    [self.user setObject:title forKey:@"buttonText"];
    //第2步：创建字典（保存消息的内容）
    NSDictionary *dic = @{@"SongsTitle":title};
    //第3步：发送消息（广播）
    //参数说明：1.消息标识  2.发送消息的对象   3.携带的消息内容（字典）
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SongsMenu" object:self userInfo:dic];
    [self dismissViewControllerAnimated:YES completion:^{
    }];
    
}

//取消按钮点击事件
- (void)disAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//全部按钮点击事件
- (void)disMissAction:(UIButton *)button
{
    [self.user setObject:button.titleLabel.text forKey:@"buttonText"];
    //第2步：创建字典（保存消息的内容）
    NSDictionary *dic = @{@"SongsTitle":button.titleLabel.text};
    //第3步：发送消息（广播）
    //参数说明：1.消息标识  2.发送消息的对象   3.携带的消息内容（字典）
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SongsMenu" object:self userInfo:dic];
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
